package ac.github.os

import ac.github.os.internal.core.action.Action
import ac.github.os.internal.core.condition.Condition
import ac.github.os.internal.core.selector.Selector
import ac.github.os.internal.core.skill.SkillScheme
import ac.github.os.internal.core.store.Store
import ac.github.os.util.listFile
import ac.github.os.util.newfolder
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common.platform.Plugin
import taboolib.common.platform.function.getDataFolder
import taboolib.common.platform.function.info
import taboolib.module.configuration.Config
import taboolib.module.configuration.SecuredFile
import java.io.File

object OriginSkill : Plugin() {

    @Config("config.yml", migrate = true)
    lateinit var config: SecuredFile

    lateinit var skillFolder: File

    val actions = arrayListOf<Action>()
    val selectors = arrayListOf<Selector>()
    val schemes = arrayListOf<SkillScheme>()
    val conditions = arrayListOf<Condition>()

    override fun onLoad() {
        skillFolder = newfolder(getDataFolder(), "skill", files = listOf("def0.yml"))
    }

    @Awake(LifeCycle.ENABLE)
    fun loadScheme() {



        schemes.clear()
        skillFolder.listFile("yml").forEach {
            val securedFile = SecuredFile.loadConfiguration(it)
            securedFile.getKeys(false).map { securedFile.getConfigurationSection(it) }.forEach {
                val holders = it.getString("holder").split(",").toList()
                val cooldown = it.getDouble("cooldown", -1.0)
                val conditions = it.getStringList("conditions") ?: arrayListOf()
                val skills = it.getStringList("skills") ?: arrayListOf()
                val skillScheme = SkillScheme(it.name, cooldown, holders, conditions, skills)
                schemes.add(skillScheme)
            }
        }
    }

    fun debug(call: () -> Unit) {
        if (config.getBoolean("options.debug",false)) {
            call()
        }
    }

    override fun onEnable() {
        info("   ____         _         _           _____  __    _  __ __")
        info("  / __ \\ _____ (_)____ _ (_)____     / ___/ / /__ (_)/ // /")
        info(" / / / // ___// // __ `// // __ \\    \\__ \\ / //_// // // / ")
        info("/ /_/ // /   / // /_/ // // / / /   ___/ // ,<  / // // /  ")
        info("\\____//_/   /_/ \\__, //_//_/ /_/   /____//_/|_|/_//_//_/   ")
        info("               /____/                                      ")
    }

}